package com.sky.service.impl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;

import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId()); // 设置当前用户的id
        // 1.当前商品是否已经存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list != null && list.size() > 0){
            // 2.存在则修改数量
            ShoppingCart item = list.get(0);
            item.setNumber(item.getNumber() + 1);
            // 更新数据
            shoppingCartMapper.update(item);
        } else {
            // TODO:  3.不存在则重新组织数据插入
            // 判断当前提交的购物车数据是套餐还是菜品
            if(shoppingCartDTO.getSetmealId() != null){
                // 套餐
                // 查询当前套餐信息，封装数据
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);
            } else {
                // 菜品
                // 查询当前菜品信息，封装数据
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);
            }
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
       Long userId =  BaseContext.getCurrentId();
       ShoppingCart shop = ShoppingCart.builder()
               .userId(userId)
               .build();
        return shoppingCartMapper.list(shop);

    }

    /**
     *  清华购物车
     */

    @Override
    public void clear() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clear(userId);
    }

    /**
     * 删除购物车数据
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 查询当前购物车信息
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list != null && list.size() > 0) {
            shoppingCart = list.get(0);
            // 判断当数量是否等于1
            Integer number = shoppingCart.getNumber();
            if(number == 1) {
                shoppingCartMapper.delete(shoppingCart.getId());
            } else {
                shoppingCart.setNumber(number - 1);
                shoppingCartMapper.update(shoppingCart);
            }
        }
    }
}
